package cn.yuemouren.security.app;

import cn.yuemouren.security.app.social.impl.AppSignInUtils;
import cn.yuemouren.security.core.social.support.SocialUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Timi
 * @Description: TODO
 * @Date: 2020/7/4 19:36
 * @Version: v1.0
 */
@RestController
public class TimiAuthenticationController {

    @Autowired
    private ProviderSignInUtils providerSignInUtils;

    @Autowired
    private AppSignInUtils appSignInUtils;

    @GetMapping("/social/signUp")
    public SocialUserInfo getSocialUserInfo(HttpServletRequest request){
        SocialUserInfo userInfo = new SocialUserInfo();
        Connection<?> connection = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request));
        userInfo.setProviderId(connection.getKey().getProviderId());
        userInfo.setUserProviderId(connection.getKey().getProviderUserId());
        userInfo.setNickname(connection.getDisplayName());
        userInfo.setHeadImage(connection.getImageUrl());

        appSignInUtils.saveConnectionData(new ServletWebRequest(request),connection.createData());
        return userInfo;
    }
}
